package com.jic.member.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.member.entity.MememberVipBase;
import com.jic.member.entity.power.MevipRule;
import com.jic.member.mapper.MememberVipBaseMapper;
import com.jic.member.mapper.power.MevipRuleMapper;
import com.jic.member.request.MemberVipRequest;
import com.jic.member.response.MemberVipResponse;
import com.jic.member.service.MememberVipBaseService;
import com.jic.common.base.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * vip基础表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-06 01:06:58
 */
@Service
@Slf4j
public class MememberVipBaseServiceImpl implements MememberVipBaseService {

    @Resource
    private MememberVipBaseMapper mememberVipBaseMapper;
    @Resource
    MevipRuleMapper mevipRuleMapper;
    @Resource
    private SysDeptApi sysDeptApi;

    @Override
    public RestResult<Long> insert(MemberVipRequest mememberVipBase) {
        RestResult result = new RestResult();
        long flag = 0;
        MememberVipBase mememberVipBase1 = new MememberVipBase();
        BeanUtils.copyProperties(mememberVipBase, mememberVipBase1);
        mememberVipBase1.setCreateTime(new Date());
        mememberVipBase1.setDeptId(mememberVipBase.getAdminOperate().getDeptId());
        mememberVipBase1.setCreatorName(mememberVipBase.getAdminOperate().getOperateName());
        mememberVipBase1.setDeleteFlag(0);
        mememberVipBase1.setDeptType(0L);
        flag = mememberVipBaseMapper.insert(mememberVipBase1);
        result.setData(flag);
        return result;
    }

    @Override
    public RestResult<Long> delete(MemberVipRequest mememberVipBase) {
        RestResult result = new RestResult();
        MevipRule mevip = new MevipRule();
        long flag = 0;

            if (mememberVipBase.getId() != null) {
                mevip.setDeleteFlag(0);
                mevip.setVipId(mememberVipBase.getId().longValue());
                List<MevipRule>mevipResult = mevipRuleMapper.query(mevip);
                if (mevipResult.size()>0){
                    result.setMsg("该等级下已关联平台权益，请解除关联后再进行操作!");
                    result.setCode("9999");
                    return result;
                }
                flag = mememberVipBaseMapper.delete(Long.valueOf(mememberVipBase.getId()));
            } else {
                log.error("删除失败，id为空");
            }

        result.setData(flag);
        return result;
    }

    @Override
    public RestResult<Long> update(MemberVipRequest mememberVipBase) {
        RestResult result = new RestResult();
        long flag = 0;
            if (mememberVipBase != null) {
                MememberVipBase mememberVipBase1 = new MememberVipBase();
                BeanUtils.copyProperties(mememberVipBase, mememberVipBase1);
                mememberVipBase1.setUpdateTime(new Date());
                mememberVipBase1.setDeptId(mememberVipBase.getAdminOperate().getDeptId());
                mememberVipBase1.setUpdaterName(mememberVipBase.getAdminOperate().getOperateName());
                flag = mememberVipBaseMapper.update(mememberVipBase1);
            } else {
                log.error("修改失败");
            }

        result.setData(flag);
        return result;
    }

    @Override
    public MememberVipBase queryByPrimaryKey(Long id) {
        return mememberVipBaseMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberVipBase> query(MememberVipBase mememberVipBase) {
        return mememberVipBaseMapper.query(mememberVipBase);
    }

    @Override
    public RestResult<PageResult<MemberVipResponse>> queryPage(MemberVipRequest memberVipRequest) {
        //计算下标
        int startIndex = (memberVipRequest.getStart() - 1) * memberVipRequest.getLimit();
        List<MemberVipResponse> list = mememberVipBaseMapper.queryPage(startIndex, memberVipRequest.getLimit(), memberVipRequest);
        list.forEach(items -> {
            RestResult<String> name = sysDeptApi.queryDeptNameById(items.getDeptId());
            items.setDeptName(name.getData());
        });
        long count = mememberVipBaseMapper.queryPageCount(memberVipRequest);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        pageResult.setRows(list);
        RestResult result = new RestResult();
        result.setData(pageResult);
        return result;
    }
}